package com.hcy.userapi.customer.controller;


import com.hcy.business.service.impl.UserServiceBzImpl;
import com.hcy.rabbitbasicmodel.enums.Code;
import com.hcy.rabbitbasicmodel.model.ResponseData;
import com.hcy.rabbitbasicmodel.signl.Select;
import com.hcy.usermodel.dto.request.CustomerInfoRequestDTO;
import com.hcy.usermodel.dto.request.EvaluationRequestDTO;
import com.hcy.usermodel.dto.request.UserAuthRequestDTO;
import com.hcy.usermodel.dto.response.CustomerInfoResponseDTO;
import com.hcy.usermodel.dto.response.ManagerPermissionResponseDTO;
import com.hcy.usermodel.dto.response.UserAuthsResponseDTO;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import sun.net.TelnetOutputStream;

/**
 * @Author huchenying
 * Description:
 * @Date 2021/11/20 13:09
 */
@RestController
@RequestMapping("/user")
public class UserController {

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private UserServiceBzImpl userServiceBz;

    @GetMapping("/getSome")
    public ResponseData<String> getSome(){
        ResponseData<String> res = new ResponseData<>();
        res.setCode(Code.Success);
        res.setMsg("请求成功");
        res.setData("我是data");
        return res;
    }

    @PostMapping("/getAuths")
    public ResponseData<UserAuthsResponseDTO> getAuthsByName(@Validated(Select.class) @RequestBody UserAuthRequestDTO requestDto, BindingResult bindingResult){
        logger.info("getListByName start param ->{}",requestDto.getUsername());
        ResponseData<UserAuthsResponseDTO> result = new ResponseData<>();
        if(bindingResult.hasErrors()){
            result.setCode(Code.Fail);
            result.setMsg("参数校验错误");
            return result;
        }
        try{
            UserAuthsResponseDTO user = userServiceBz.getAuthsByName(requestDto);
            logger.info("getListByName end result -> {}", ToStringBuilder.reflectionToString(user));
            return result.success(user);
        }catch (Exception e){
            return result.exception();
        }
    }

    /**
     * @Author huchenying
     * @Description  通过用户名和businessCode 获取用户的基础信息
     * @Date 2022/4/23 1:11
     **/
    @PostMapping("/getUserPermissionInfo")
    public ResponseData<ManagerPermissionResponseDTO> getUserPermissionInfo(@Validated(Select.class) @RequestBody UserAuthRequestDTO requestDto){
        logger.info("getUserPermissionInfo start param ->{}",requestDto.getUsername());
        ResponseData<ManagerPermissionResponseDTO> result = new ResponseData<>();
        try{
            ManagerPermissionResponseDTO user = userServiceBz.getUserRoleMenu(requestDto);
            logger.info("getUserPermissionInfo end result -> {}", ToStringBuilder.reflectionToString(user));
            return result.success(user);
        }catch (Exception e){
            logger.error("e -> {}", ToStringBuilder.reflectionToString(e));
            return result.fail("获取用户权限信息失败");
        }
    }

    @PostMapping("/add")
    public ResponseData<UserAuthsResponseDTO> addTestUser(@RequestBody UserAuthRequestDTO user){
        ResponseData<UserAuthsResponseDTO> res = new ResponseData<>();
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        userServiceBz.addTestUser(user);
        res.setCode(Code.Success);
        res.setMsg("ok");
        return res;
    }

    /**
     * @Author huchenying
     * @Description 更改余额
     * @Date 2022/4/7 15:13
     **/
    @PostMapping("/changeCoin")
    public ResponseData<CustomerInfoResponseDTO> changeCoin(@RequestBody CustomerInfoRequestDTO requestDTO){
        logger.info("changeCoin start param -> {}",ToStringBuilder.reflectionToString(requestDTO));
        ResponseData<CustomerInfoResponseDTO> responseData = new ResponseData<>();
        try {
            CustomerInfoResponseDTO responseDTO = userServiceBz.changeCoin(requestDTO.getPhone(),requestDTO.getChangeCoin());
            responseData.setCode(Code.Success);
            responseData.setData(responseDTO);
        } catch (Exception e) {
            logger.error("更改余额异常 e-> {}",ToStringBuilder.reflectionToString(e));
            responseData.setCode(Code.Fail);
            responseData.setMsg("更改余额失败");
        }
        logger.info("changeCoin res -> {}",ToStringBuilder.reflectionToString(responseData));
        return responseData;
    }

    /**
     * @Author huchenying
     * @Description 评价
     * @Date 2022/4/7 15:13
     **/
    @PostMapping("/evaluation")
    public ResponseData<Object> evaluation(@RequestBody EvaluationRequestDTO requestDTO){
        logger.info("evaluation start param -> {}",ToStringBuilder.reflectionToString(requestDTO));
        ResponseData<Object> responseData = new ResponseData<>();
        try {
            userServiceBz.evaluation(requestDTO);
            responseData.setCode(Code.Success);
        } catch (Exception e) {
            logger.error("评价 e-> {}",ToStringBuilder.reflectionToString(e));
            responseData.setCode(Code.Fail);
            responseData.setMsg("评价失败");
        }
        return responseData;
    }

    /**
     * @Author huchenying
     * @Description baseInfo
     * @Date 2022/4/7 16:36
     **/
    @PostMapping("/baseInfo")
    public ResponseData<CustomerInfoResponseDTO> baseInfo(@RequestBody CustomerInfoRequestDTO requestDTO){
        logger.info("baseInfo start param -> {}",ToStringBuilder.reflectionToString(requestDTO));
        ResponseData<CustomerInfoResponseDTO> responseData = new ResponseData<>();
        try {
            CustomerInfoResponseDTO responseDTO = userServiceBz.getBaseInfo(requestDTO.getPhone());
            responseData.setCode(Code.Success);
            responseData.setData(responseDTO);
        } catch (Exception e) {
            logger.error("e-> {}",ToStringBuilder.reflectionToString(e));
            responseData.setCode(Code.Fail);
            responseData.setMsg("获取用户信息失败");
        }
        logger.info("res -> {}",ToStringBuilder.reflectionToString(responseData));
        return responseData;
    }
}
